package com.thx.log.action;

import com.thx.common.struts2.CurdAction;
import com.thx.common.web.ActionResult;
import com.thx.log.LogUtil;
import com.thx.log.model.Log;
import com.thx.log.service.LogManager;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Namespace("/sys")
@Action(value = "log", results = {
		@Result(name = "list", location = "/pages/system/log/list.jsp"),
        @Result(name = "view", location = "/pages/system/log/detail.jsp"),
        @Result(name = "json", type = "json", params = {"root", "result"})
})
@Controller
@Scope("prototype")
public class LogAction extends CurdAction<Log> {

	private static final long serialVersionUID = 1L;

	protected String dateRange;//时间区间

	@Autowired
	protected LogManager logMgr;

	protected void doList() throws Exception {
		String startTime = "";
		String endTime = "";
		if(StringUtils.isNotBlank(dateRange)){
			String[] dates = dateRange.split("-");
			startTime = dates[0].replaceAll("/", "-");
			endTime = dates[1].replaceAll("/", "-");
		}
		list = logMgr.searchLogs(entity.getUserName(), startTime, endTime);
	}

	protected void doView() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			entity = logMgr.get(id);
			if (entity == null) {
				setResult(ActionResult.FAIL, "要查看的记录不存在！！！");
			}
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	protected void doDelete() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			logMgr.delete(id);
			setResult(ActionResult.SUCCESS, "删除成功！");
			LogUtil.log("日志模块", LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除日志");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	protected void doDelMulti() throws Exception {
		if (delId != null && delId.length > 0) {
			delId = delId[0].split(",");
			logMgr.delMulti(delId);
			setResult(ActionResult.SUCCESS, "删除成功！");
			LogUtil.log("日志模块", LogUtil.ACTION_DELETE, LogUtil.RESULT_SUCCESS, "删除日志");
		} else {
			setResult(ActionResult.FAIL, "服务器异常！");
		}
	}

	public String getDateRange() {
		return dateRange;
	}

	public void setDateRange(String dateRange) {
		this.dateRange = dateRange;
	}

}
